@extends('layouts.back_stage')
@section('content')
    <style>
        .inp_art,.inp_price{width:150px;height:30px;line-height: 30px;font-size: 14px;outline: none;border: 1px solid #cfdada;background: #fff;border-radius: 2px;padding: 4px 8px;color: #555;box-shadow: none;}
        .technology_li{margin-bottom:10px;}
        .technology_li span{margin-right:20px;font-size:14px;}
    </style>
    <div class="container">
        <div style="padding:20px 20px 20px 0;overflow:hidden;">
            <h3 style="float:left;">工艺加工费用</h3>
            <button class="btn btn-primary " onclick="history.go(-1)" style='float:right'>返回上一页</button>
        </div>
        <div class="form-group"><button type="button" class="upAdd btn btn-primary">+</button></div>
        <div class="form-group">
            <ul class="technology">
                @foreach($infos as $k => $v )
                <li class='technology_li'>
                   <span class='inp_box'><b>工艺：</b><select class='inp_art'>
                           @foreach($craft_code as $kk => $vv)
                               <option value='{{$vv->id}}'
                                       @if($v->craft_code_id== $vv->id)
                                       selected
                                       @endif
                               >{{$vv->craft_name}}</option>
                           @endforeach
                               {{--<option value=''>请选择</option>--}}
                   </select></span><span class='inp_mo'><b>价格：</b><input type='text' class='inp_price' value='{{$v->price}}' maxlength="8" placeholder='请填写价格' onkeyup="clearNoNum(this)"></span><span><button type='button' class='btn btn-success sav_bt'>保存</button></span><span><button type='button' class='btn btn-danger del_bt'>删除</button></span>
                </li>
                @endforeach
            </ul>
        </div>
    </div>
    <script>
        var id = '{{ $_GET['id'] }}';
        /*价格*/
        function clearNoNum(obj){
            obj.value = obj.value.replace(/[^\d.]/g,"");
            obj.value = obj.value.replace(/\.{2,}/g,".");
            obj.value = obj.value.replace(/^\./g,"");
            obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
            obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3');
            if(obj.value.indexOf(".")< 0 && obj.value !=""){
                obj.value= parseFloat(obj.value);
            }
        }
        /*工艺加*/
        $(".upAdd").click(function(){
            $.ajax({
                url:'/storage/craft_code_all',
                type:'post',
                data:{

                },
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                },
                dataType:'json',
                success:function(data){
                    var string='';
                    if(data.success){
                        for(var i in data.info){
                            string+='<option value='+i+'>'+data.info[i]+'</option>';
                        }
                        var str="<li class='technology_li'>" +
                                    "<span class='inp_box'><b>工艺：</b>" +
                                "<select class='inp_art'>"+
                                "<option value=''>请选择</option>"+
                                "</select></span>"+
                                "<span class='inp_mo'><b>价格：</b><input type='text' class='inp_price' value='' maxlength='8' placeholder='请填写价格' onkeyup='clearNoNum(this)'></span>"+
                                "<span><button type='button' class='btn btn-success sav_bt'>保存</button></span>"+
                                "<span><button type='button' class='btn btn-danger del_bt'>删除</button></span>"+
                                "</li>";
                        $(".technology").append(str);
                        $(".inp_art").append(string);
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            });
        });
        /*工艺删除*/
        $(document).on("click",".del_bt",function(){
            var that=$(this);
            var code_id=$(this).parent().siblings('.inp_box').children(".inp_art").val();
            var monmey=$(this).parent().siblings('.inp_mo').children(".inp_price").val();
            if(code_id==''|| monmey==''){
                $(this).parent().parent().remove();
            }else{
                $.ajax({
                    url:'/storage/product_cost_craft',
                    type:'post',
                    data:{
                        'id':id,
                        'craft_code_id':code_id,
                        'price':monmey,
                        'type':-1
                    },
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')
                    },
                    dataType:'json',
                    success:function(data){
                        if(data.success){
                            that.parent().parent().remove();
                            $(".faceImg").attr("src","/img/laugh.png");
                            $(".oHandle").text("删除成功");
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }else{
                            $(".faceImg").attr("src","/img/weep.png");
                            $(".oHandle").text(data.msg);
                            $(".oSuccess").show(0).delay(1000).hide(0);
                        }
                    }
                })
            }
        });
        /*工艺提交*/
        $(document).on("click",".sav_bt",function(){
            var craft_code_id=$(this).parent().siblings('.inp_box').children(".inp_art").val();
            var price=$(this).parent().siblings('.inp_mo').children(".inp_price").val();
            if(craft_code_id==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请选择工艺");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            if(price==''){
                $(".faceImg").attr("src","/img/weep.png");
                $(".oHandle").text("请填写工艺价格");
                $(".oSuccess").show(0).delay(1000).hide(0);
                return false;
            }
            $.ajax({
                url:'/storage/product_cost_craft',
                type:'post',
                data:{
                    'id':id,
                    'craft_code_id':craft_code_id,
                    'price':price,
                    'type':1
                },
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content')},
                dataType:'json',
                success:function(data){
                    if(data.success){
                        $(".faceImg").attr("src","/img/laugh.png");
                        $(".oHandle").text("保存成功");
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }else{
                        $(".faceImg").attr("src","/img/weep.png");
                        $(".oHandle").text(data.msg);
                        $(".oSuccess").show(0).delay(1000).hide(0);
                    }
                }
            })
        });
    </script>
@endsection